package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesCompanyCategoryReportQueryRepository;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesCompanyCategoryReportLoadListRequest;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesCompanyCategoryReportLoadListVO;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryMonthSalesReportRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalescompanycategoryreport.model.MonthSalesCompanyCategoryReport;
import com.epoch.app.otsapplicationreportsdk.monthsalescompanycategoryreport.service.MonthSalesCompanyCategoryReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/12/10 23:08
 */
@Component
public class MonthSalesCompanyCategoryReportQueryRepositoryImpl implements MonthSalesCompanyCategoryReportQueryRepository {

    @Resource
    MonthSalesCompanyCategoryReportService monthSalesCompanyCategoryReportService;

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryList(QueryMonthSalesReportRequest request) {
        MonthSalesCompanyCategoryReportLoadListVO monthSalesCompanyCategoryReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesCompanyCategoryReport>> list = monthSalesCompanyCategoryReportService.loadListForPage(monthSalesCompanyCategoryReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryMonthSalesCompanyCategoryReportSortList(QueryMonthSalesReportRequest request) {
        MonthSalesCompanyCategoryReportLoadListVO monthSalesCompanyCategoryReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesCompanyCategoryReport>> list = monthSalesCompanyCategoryReportService.loadListForPageSort(monthSalesCompanyCategoryReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public List<MonthSalesBasicReportSDO> getCategoryReportList(QueryMonthSalesReportRequest request) {
        MonthSalesCompanyCategoryReportLoadListRequest queryRequest = getListRequestConverter(request);
        Result<List<MonthSalesCompanyCategoryReport>> list = monthSalesCompanyCategoryReportService.loadList(queryRequest);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<MonthSalesBasicReportSDO> statisticalQuery(QueryMonthSalesReportRequest request) {
        MonthSalesCompanyCategoryReportLoadListVO monthSalesCompanyCategoryReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesCompanyCategoryReport>> response = monthSalesCompanyCategoryReportService.statisticalQuery(monthSalesCompanyCategoryReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    private MonthSalesCompanyCategoryReportLoadListVO loadListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesCompanyCategoryReportLoadListVO listVO = MonthSalesCompanyCategoryReportLoadListVO.newMonthSalesCompanyCategoryReportLoadListVOBuilder()
                .orgBusinessUnitIds(request.getOrgBusinessUnitIds())
                .scItemCategoryIds(request.getScItemCategoryIds())
                .bizType(request.getBizType())
                .statisticalDate(request.getStatisticalDate())
                .dataMark(request.getDataMark())
                .groupBy(request.getGroupBy())
                .build();
        listVO.setStart(request.getStart());
        listVO.setLimit(request.getLimit());
        return listVO;
    }

    private MonthSalesCompanyCategoryReportLoadListRequest getListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesCompanyCategoryReportLoadListRequest listRequest = MonthSalesCompanyCategoryReportLoadListRequest.newMonthSalesCompanyCategoryReportLoadListRequestBuilder()
                .orgBusinessUnitId(request.getOrgBusinessUnitId())
                .bizType(request.getBizType())
                .scItemCategoryId(request.getScItemCategoryId())
                .planTime(request.getPlanMonth())
                .statisticalDate(request.getStatisticalDate())
                .build();
        return listRequest;
    }

}
